﻿/*
Copyright (c) 2012 Vratislav Cermak (vratacermak@gmail.com)
 
This file is part of TeachMe Project. See http://code.google.com/p/teach-me for more information.
 
TeachMe is an open source project. Using of TeachMe is free of charge.
It is distributed under GNU General Public License version 3 (GPLv3).
GPLv3 can be found at http://www.gnu.org/licenses/gpl.html.
 
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the license for more details.
*/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TeachMe.Application.Properties;
using System.IO;

namespace TeachMe.Application.Forms.Options
{
    public partial class Sounds : OptionsControl
    {
        public Sounds()
        {
            InitializeComponent();
        }

        internal override void LoadOptions()
        {
            switch (Root.UserSettings.SoundConfiguration.Value)
            {
                case SoundConfiguration.Muted:
                    rbMute.Checked = true;
                    break;
                case SoundConfiguration.Default:
                    rbDefault.Checked = true;
                    break;
                case SoundConfiguration.Custom:
                    rbCustom.Checked = true;
                    break;
            }

            tbEntryDisplayedPath.Text = Root.MachineSettings.EntryDisplayedSoundPath;

            UpdateControls();
        }

        private void UpdateControls()
        {
            txEntryDisplayed.Enabled = rbCustom.Checked;
            tbEntryDisplayedPath.Enabled = rbCustom.Checked;
            bEntryDisplayedBrowse.Enabled = rbCustom.Checked;
            bEntryDisplayedPreview.Enabled = rbCustom.Checked;
        }

        internal override void SaveOptions()
        {
            Root.MachineSettings.EntryDisplayedSoundPath = tbEntryDisplayedPath.Text;

            if (rbMute.Checked)
            {
                Root.UserSettings.SoundConfiguration.Value = SoundConfiguration.Muted;
            }
            else if (rbDefault.Checked)
            {
                Root.UserSettings.SoundConfiguration.Value = SoundConfiguration.Default;
            }
            else if (rbCustom.Checked)
            {
                Root.UserSettings.SoundConfiguration.Value = SoundConfiguration.Custom;
            }
        }

        private void OnRadioButtonsChanged(object sender, EventArgs e)
        {
            UpdateControls();
        }

        private void OnBrowseClick(object sender, EventArgs e)
        {
            odfOpenFile.AddExtension = false;
            odfOpenFile.CheckFileExists = true;
            odfOpenFile.Filter = "WAV files|*.wav";
            odfOpenFile.Multiselect = false;
            odfOpenFile.ShowReadOnly = true;

            odfOpenFile.FileName = tbEntryDisplayedPath.Text;

            if (!File.Exists(tbEntryDisplayedPath.Text))
            {
                if (Directory.Exists(Root.MachineSettings.LastBrowseDirectory))
                {
                    odfOpenFile.InitialDirectory = Root.MachineSettings.LastBrowseDirectory;
                }
            }

            if (odfOpenFile.ShowDialog() == DialogResult.OK)
            {
                tbEntryDisplayedPath.Text = odfOpenFile.FileName;

                using (Root.MachineSettings.Edit())
                {
                    Root.MachineSettings.LastBrowseDirectory = Path.GetDirectoryName(odfOpenFile.FileName);
                }
            }
        }

        private void OnEntryDisplayedPreviewClick(object sender, EventArgs e)
        {
            if (!Utilities.PlayWaveFromFile(tbEntryDisplayedPath.Text))
            {
                MessageBox.Show(Resources.ErrorPlayingSound, Resources.MessageBoxCaption, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
